package com.learn.sb.sblearnshardingjdbc.strategy;


import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;

@Slf4j
public class UseridDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

	public final static int SHARDING_NUMBER = 2;

	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<java.lang.Integer> preciseShardingValue) {
		for (String name : availableTargetNames) {
			if(name.endsWith("_" + preciseShardingValue.getValue() % SHARDING_NUMBER)) {
				log.info("database : " + name);
				return name;
			}
		}
		return null;
	}
}
